package com.iheqing.db.core.generator;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


@Slf4j
public class MysqlGenerator {

    /**
     * RUN THIS
     */
    public static void main(String[] args) {
        String module = "test-app";
        String basePackage = "com.iheqing";
        String tablePrefix = "sys_";
        String [] includeTable = {"sys_user","sys_menu"};
        String projectPath = "E:/workspace_2020/hq-builder/base-application/"+module+"/";
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(projectPath + "src/main/java");
        gc.setAuthor("iheqing@qq.com");
//        gc.setFileOverride(true);
        gc.setOpen(false);

        gc.setIdType(IdType.AUTO);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://118.24.47.93:13306/mybooks?useUnicode=true&useSSL=false&characterEncoding=utf8");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("Qa27101783");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(module.replaceAll("-","."));
        pc.setParent(basePackage+"."+module.replaceAll("-","."));
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing

            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                String outXMLMapperFileName =  projectPath + "/src/main/resources/"+basePackage.replaceAll("\\.","/")+"/"+module.replaceAll("-","/")+"/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
                return outXMLMapperFileName;
            }
        });
        focList.add(new FileOutConfig("/templates/mapper.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                String outJavaMapperFileName =  projectPath + "/src/main/java/"+basePackage.replaceAll("\\.","/")+"/"+module.replaceAll("-","/")+"/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_JAVA;
                return outJavaMapperFileName;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("com.mrcoder.sbmpmultidb.BaseEntity");
        strategy.setEntityLombokModel(true);
//        strategy.setSuperMapperClass()
        //strategy.setSuperControllerClass("com.mrcoder.sbmpmultidb.BaseController");
        strategy.setInclude(includeTable);
//        strategy.setSuperEntityColumns(primaryKeyIds);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(tablePrefix);
        mpg.setStrategy(strategy);
        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
